/*****************************************************
*                                                                          
*               COPYRIGHT (c) 2015-2017 Hargic Corporation              
*                         All Rights Reserved                              
*                                                                          
* The source code contained or described herein and all documents          
* related to the source code ("Material") are owned by Hargic         
* Corporation or its licensors.  Title to the Material remains             
* with Hargic Corporation or its suppliers and licensors.               
*                                                                          
* The Material is protected by worldwide copyright and trade secret        
* laws and treaty provisions. No part of the Material may be used,         
* copied, reproduced, modified, published, uploaded, posted, transmitted,  
* distributed, or disclosed in any way except in accordance with the       
* applicable license agreement.                                            
*                                                                          
* No license under any patent, copyright, trade secret or other            
* intellectual property right is granted to or conferred upon you by       
* disclosure or delivery of the Materials, either expressly, by            
* implication, inducement, estoppel, except in accordance with the         
* applicable license agreement.                                            
*                                                                          
* Unless otherwise agreed by Hargic in writing, you may not remove or   
* alter this notice or any other notice embedded in Materials by Hargic 
* or Hargic's suppliers or licensors in any way.                        
*                                                                          
** Author: ted.huang
** Date: 2018-02-25
*
*****************************************************/

/***** Include files ********************************/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <bcm2835.h>
#include "spi_if.h"
#include "cc1101.h"
#include "rf433.h"

/***** Defines **************************************/

#define CC1101_GDO0_PIN         25
#define CC1101_GDO2_PIN         24
#define CC1101_CSN_PIN          23

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/

const char teststr[] = "hello, world!\r\n";

/***** Define Prototype of functions ****************/

static int cc1101_gpio_init(void)
{
    if (!bcm2835_init())
    {
        printf("bcm2835_init failed. Are you running as root??\n");
        return -1;
    }

    bcm2835_gpio_fsel(CC1101_CSN_PIN, BCM2835_GPIO_FSEL_OUTP);
    bcm2835_gpio_fsel(CC1101_GDO0_PIN, BCM2835_GPIO_FSEL_INPT);
    bcm2835_gpio_fsel(CC1101_GDO2_PIN, BCM2835_GPIO_FSEL_INPT);
    return 0;
}

static unsigned char cc1101_spi_transfer(unsigned char val)
{
    return spi_transfer(val);
}

static void cc1101_spi_csn(unsigned char level)
{
    if (level == CC1101_SPI_CSN_LEVEL_LOW)
        bcm2835_gpio_write(CC1101_CSN_PIN, LOW);
    else
        bcm2835_gpio_write(CC1101_CSN_PIN, HIGH);
}

static int cc1101_gdo0_input(void)
{
    return bcm2835_gpio_lev(CC1101_GDO0_PIN);
}

static int cc1101_gdo2_input(void)
{
    return bcm2835_gpio_lev(CC1101_GDO2_PIN);
}

static void cc1101_delay_ms(unsigned int ms)
{
    bcm2835_delay(ms);
}

static void print_dump_hex(char *str, unsigned char *data, unsigned int len)
{
    printf("%s dump hex %d len:", str, len);
    for (int i=0; i<len; i++)
    {
        if (i % 16 == 0)
            printf("\n%p:", &data[i]);

        printf(" %02x", data[i]);
    }
    printf("\n");
}


/**
 * @brief
 * @param  None
 * @retval None
 */
int main(int argc, char **argv)
{
	cc1101_func_t func;
    unsigned char recv_buff[128];
    int recvlen;

    if (cc1101_gpio_init() != 0)
    {
        printf("gpio init failed\n");
        return -1;
    }

	if (spi_init() != 0)
    {
		printf("spi init failed.\n");
		return -1;
    }

    func.spi_transfer_byte  = cc1101_spi_transfer;
    func.spi_csn_level      = cc1101_spi_csn;
    func.gdo0_input         = cc1101_gdo0_input;
    func.gdo2_input         = cc1101_gdo2_input;
    func.delay_ms           = cc1101_delay_ms;
    cc1101_register_spi_func(&func);

    CC1101_Init();
    // rf433_init();

    while (1)
    {
#if 0
        printf("%s: start CC1101_Tx_Packet\n", __func__);
        CC1101_Tx_Packet((unsigned char *)teststr, strlen(teststr), ADDRESS_CHECK);
        printf("sending '%s'...\n", teststr);
        // CC1101_Set_Mode(RX_MODE);
        bcm2835_delay(1000);
#else
        CC1101_Clear_RxBuffer();
        CC1101_Set_Mode(RX_MODE);
        recvlen = CC1101_Rx_Packet(recv_buff, 128, 1200);
        printf("%s: recv %d rssi %d\n", __func__, recvlen, cc1101_get_rssi());
        if (recvlen != 0)
        {
            // printf("%s: recv %.*s\n", __func__, recvlen, recv_buff);
            // rf433_msg_handler(recv_buff, recvlen);

            print_dump_hex("recv cc1101", recv_buff, recvlen);
            // cc1101_dump_register();

            CC1101_Tx_Packet((unsigned char *)teststr, strlen(teststr), ADDRESS_CHECK);
            printf("sending '%s'...\n", teststr);
            CC1101_Set_Mode(RX_MODE);
        }
#endif
    }

    spi_de_init();
    return 0;
}


/* End of file */



